package expression;

import java.util.Scanner;
import java.util.StringTokenizer;

public class Espressione {

	private String linea;

	public Espressione(String Linea) {

		this.linea = Linea;

	}

	public int valutaEspressione() {
		StringTokenizer st = new StringTokenizer(linea, "+-/*()", true);

		return valutaEspressione(st);
	}

	private int valutaEspressione(StringTokenizer st) {

		int ris = valutaOperando(st);
		while (st.hasMoreTokens()) {
			char op = st.nextToken().charAt(0);
			if (op == ')')
				return ris;

			int opnd = valutaOperando(st);
			switch (op) {
			case '+':
				ris += opnd;
				break;
			case '-':
				ris -= opnd;
				break;
			case '*':
				ris *= opnd;
				break;
			case '/':
				ris /= opnd;
				break;

			default:
				throw new RuntimeException();
			}
		}
		return ris;

	}

	public String toString() {
		return linea;

	}

	public boolean equals(Object x) {
		if (!(x instanceof Espressione))
			return false;

		if (x == this)
			return true;
		Espressione e = (Espressione) x;
		return this.linea.equals(e.linea);
	}

	public int hashCode() {
		return linea.hashCode();
	}

	private int valutaOperando(StringTokenizer st) {

		String tk = st.nextToken();

		if (tk.equals("("))

			return valutaEspressione(st);

		int opnd = Integer.parseInt(tk);
		return opnd;
	}

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);

		while (true) {
			System.out.print("> ");
			String linea = sc.nextLine();
			if (linea.equals("."))
				break;
			try {
				int ris = new Espressione(linea).valutaEspressione();
				System.out.println(linea + "=" + ris);

			} catch (Exception e) {
				System.out.println("Espressione malformata");

			}
		}// while
		sc.close();

		System.out.println("Bye.");

	}
}
